package com.feicheng.controller.main.admin.store;


import com.alibaba.fastjson.JSONObject;
import com.feicheng.common.constant.Constant;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.service.IFHomeService;
import com.feicheng.service.IFStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cailongyang
 * @since 2021-12-02
 */
@RestController
@RequestMapping("/main/admin/store/store")
@CrossOrigin
public class StoreController {

    @Autowired
    IFStoreService fStoreService;

    @Autowired
    IFHomeService fHomeService;

    @PutMapping("/{storeId}/open")
    public ResultInfo openStore(@PathVariable(name="storeId") Integer storeId) {
        if (null == storeId) {
            // TODO: 门店ID缺失
            throw new SystemException("门店ID缺失");
        }

        return fStoreService.openStore(storeId);

    }

    @PutMapping("/{storeId}/close")
    public ResultInfo closeStore(@PathVariable(name="storeId") Integer storeId) {

        if (null == storeId) {
            // TODO: 门店ID缺失
            throw new SystemException("门店ID缺失");
        }

        return fStoreService.closeStore(storeId);
    }

    @GetMapping("/list")
    public ResultInfo getStoresByPage(String
            param,
            @RequestParam(defaultValue="1") Integer current,
            @RequestParam(defaultValue="10")Integer size
    ) {
        JSONObject result = new JSONObject();
        result = JSONObject.parseObject(param);
        result.put(Constant.HTTP.PARAM.CURRENT,current);
        result.put(Constant.HTTP.PARAM.SIZE,size);
        return fStoreService.getStoresByPage(result);
    }
    //ok
    @GetMapping("/info")
    public ResultInfo getStores() {
        return fStoreService.getStores(new JSONObject());
    }
    //ok
    @GetMapping("/{storeId}/get")
    public ResultInfo getStoreInfo(@PathVariable(name="storeId") Integer storeId) {
        return fStoreService.getStoreInfo(storeId);
    }
    //ok
    @PostMapping("/info")
    public ResultInfo newStore(String param) {
        return fStoreService.newStore(JSONObject.parseObject(param));
    }
    //ok
    @PutMapping("/info")
    public ResultInfo updateStore(String param) {
        return fStoreService.updateStore(JSONObject.parseObject(param));
    }
    //ok
    @DeleteMapping("/info")
    public ResultInfo deleteStore(Integer storeId) {
        return fStoreService.deleteStore(storeId);
    }

}
